package cn.itcast.service.impl;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import sun.swing.BakedArrayList;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void upload(List<OrderSetting> osList) {
        // 判断之前日期是否存在信息, 不存在添加, 存在修改
        if (osList!=null&&osList.size()!=0) {
            for (OrderSetting orderSetting : osList) {
                Date orderDate = orderSetting.getOrderDate();
                long count = orderSettingDao.findCount(orderDate);
                if (count > 0) {
                    // 修改
                    orderSettingDao.update(orderSetting);
                } else {
                    // 添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> find(String date) {
        String[] split = date.split("-");
        // 1-9月
        if (split[1].length()==1) {
            date = split[0] + "-0" + split[1] + "%";
        }
        // 10-12月
        date = date+"%";
        List<OrderSetting> list = orderSettingDao.find(date);
        // 因为前端要日期数字,不要具体的date,所以不能直接封装list返回
        // 前端模型变量:{ date: 1, number: 120, reservations: 1 },
        List<Map> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public void editNumber(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        long count = orderSettingDao.findCount(orderDate);
        if (count==0){
            // 添加
            orderSettingDao.add(orderSetting);
        }else {
            // 修改
            orderSettingDao.update(orderSetting);
        }
    }
}
